package com.project.controller;


import com.project.bean.EmployeeBean;
import com.project.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

//    @RequestMapping("/test")
//    public String testThy(HttpServletRequest request){
//        //在request请求对象中，绑定共享数据，数据值存在于一次请求范围内
//        request.setAttribute("warning","成都疫情出现，带好口罩");
//        return "/html/test.html";
//    }

//    @RequestMapping("/test")
//    public String testThy(Model model){
//        //在请求范围内绑定共享数据，等同于request.setAttribute()
//        model.addAttribute("warning","大家不要慌，淡定");
//        return "/html/test.html";
//    }

    @RequestMapping("/test")
    public ModelAndView testThy(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("warning","相信政府，相信党");
        mv.addObject("user",new UserBean(1,"tom","13555984644", LocalDate.parse("1995-05-31")));
        mv.addObject("employee",new EmployeeBean(1,"rose",5000,"在职"));
        mv.addObject("emList", Arrays.asList(new EmployeeBean(2,"timi",10000,"在职"),
                new EmployeeBean(3,"tick",8000,"离职"),new EmployeeBean(4,"rise",25000,"在职")));
        Map<Integer,EmployeeBean> map = new HashMap<>();
        map.put(7,new EmployeeBean(7,"timi",10000,"在职"));
        map.put(5,new EmployeeBean(5,"ti",12000,"在职"));
        map.put(6,new EmployeeBean(6,"tim",8000,"离职"));
        mv.addObject("emMap",map);
        mv.addObject("num",5);
        mv.addObject("imgPath","1.jpg");
        mv.setViewName("/html/test.html");
        return mv;
    }

    @RequestMapping("/emDel")
    @ResponseBody
    public String del(int id){

        return "删除id=" + id + "的员工";
    }

}
